﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using System.Data.SqlClient;

namespace GUI
{
    public partial class usBoKichDien : UserControl
    {
        public usBoKichDien()
        {
            InitializeComponent();
        }

        private void usBoKichDien_Load(object sender, EventArgs e)
        {
            LoadLoaiBoKD();
            LoadBoKD();
            SetupDataGridView(gvCDD);
        }

        private void TrangThai(bool status)
        {
            //tsThem.Enabled = status;
            btnThem.Visible = !status;
            btnSua.Visible = status;
        }

        private void LoadLoaiBoKD()
        {
            LoaiBoKichDienBUS loaiKD_BUS = new LoaiBoKichDienBUS();
            cbbLoaiTB.DataSource = loaiKD_BUS.LoadLoaiBoKD();
            cbbLoaiTB.DisplayMember = "TenLoai";
            cbbLoaiTB.ValueMember = "MaLoai";
        }

        private void LoadBoKD()
        {
            ThietBiDienBUS BoKD_BUS = new ThietBiDienBUS();
            gvCDD.DataSource = BoKD_BUS.LoadBoKichDien();

            SetupDataGridView(gvCDD);
        }

        private void reset()
        {
            dtpNgayNhap.Text = (DateTime.Now).ToString();
            txtMaTB.Text = "";
            txtTenTB.Text = "";
            txtCongSuat.Text = "";
            txtGia.Text = "0";
            cbbLoaiTB.SelectedValue = 1;

            LoadBoKD();
        }

        private void SetupDataGridView(DataGridView gv)
        {
            DataGridViewCellStyle style = gv.ColumnHeadersDefaultCellStyle;
            style.BackColor = Color.Navy;
            style.ForeColor = Color.White;
            style.Font = new Font(gv.Font, FontStyle.Bold);


            gv.GridColor = Color.Blue;  //' Màu các của lưới
            gv.BackgroundColor = Color.AliceBlue; //' Màu nền gridview
            gv.ForeColor = Color.Green; //' Màu chữ
            //' Thiết lập màu chữ cho những dòng có chỉ số lẻ
            gv.AlternatingRowsDefaultCellStyle.ForeColor = Color.Purple;
        }

        private void THEMBOKICHDIEN()
        {
            BoKichDienDTO bkdDTO = new BoKichDienDTO();

            bkdDTO.STT = 0;
            bkdDTO.VOL = 0;
            bkdDTO.GHICHU = "";

            bkdDTO.MATB = txtMaTB.Text;
            bkdDTO.TENTB = txtTenTB.Text.Trim();
            bkdDTO.NGAYNHAP = Convert.ToDateTime(dtpNgayNhap.Text);
            bkdDTO.CSKHADUNG = int.Parse(txtCongSuat.Text);
            bkdDTO.DONGIA = 0;
            if(!string.IsNullOrEmpty(txtGia.Text))
                bkdDTO.DONGIA = int.Parse(txtGia.Text);
            bkdDTO.MALOAI = int.Parse(cbbLoaiTB.SelectedValue.ToString());

            BoKichDienBUS bkdBUS = new BoKichDienBUS();
            bkdBUS.THEMBOKICHDIEN(bkdDTO);
            reset();
        }

        private void SUABOKICHDIEN()
        {
            BoKichDienDTO bkdDTO = new BoKichDienDTO();

            bkdDTO.STT = bkdDTO1.STT;
            bkdDTO.VOL = 0;
            bkdDTO.GHICHU = "";

            bkdDTO.MATB = txtMaTB.Text;
            bkdDTO.TENTB = txtTenTB.Text.Trim();
            bkdDTO.NGAYNHAP = Convert.ToDateTime(dtpNgayNhap.Text);
            bkdDTO.CSKHADUNG = int.Parse(txtCongSuat.Text);
            bkdDTO.DONGIA = 0;
            if (!string.IsNullOrEmpty(txtGia.Text))
                bkdDTO.DONGIA = int.Parse(txtGia.Text);
            bkdDTO.MALOAI = int.Parse(cbbLoaiTB.SelectedValue.ToString());

            BoKichDienBUS bkdBUS = new BoKichDienBUS();
            bkdBUS.SuaBoKichDien(bkdDTO);
            int alert = bkdBUS.SuaBoKichDien(bkdDTO);
            reset();

            if (alert == 1)
            {
                TrangThai(false);
                MessageBox.Show("Đã sửa bộ kích điện" + bkdDTO.STT.ToString() + "thành công");
            }
        }

        private int KiemTraNhap()
        {
            if (string.IsNullOrEmpty(txtCongSuat.Text))
            {
                MessageBox.Show("Nhập đầy đủ thông tin (*) cần thiết", "Thông Báo!!");
                return 0;
            }
            return 1;
        }

        private void tsThem_Click(object sender, EventArgs e)
        {
            tsThem.Enabled = false;
            tsLoaiBoKD.Enabled = true;
            //tsDongGrbTT.Enabled = true;

            pnThongTin.Visible = true;
            pnLoai.Visible = false;
            grbBoChuyenDoiDien.Visible = true;
            TrangThai(false);
        }

        private void TrangThai_LoaiKD(bool status)
        {
            pnThongTin.Visible = status;
            grbBoChuyenDoiDien.Visible = status;
            pnLoai.Visible = !status;
        }

        private void tsLoaiBoKD_Click(object sender, EventArgs e)
        {
            tsThem.Enabled = true;
            tsLoaiBoKD.Enabled = false;
            //tsDongGrbTT.Enabled = true;

            TrangThai_LoaiKD(false);

            pnLoai.Dock = DockStyle.Fill;
            LoadLoaiBoKD_GV();
            //reset();

            btnThemLoai.Visible = true;
            btnSuaLoai.Visible = false;
        }

        private void tsDongGrbTT_Click(object sender, EventArgs e)
        {
            tsThem.Enabled = true;
            tsLoaiBoKD.Enabled = true;
            tsDongGrbTT.Enabled = true;

            pnThongTin.Visible = false;
            grbBoChuyenDoiDien.Visible = true;
            pnLoai.Visible = false;
            //TrangThai_LoaiKD(true);
            reset();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            SUABOKICHDIEN();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (KiemTraNhap() == 1)
                THEMBOKICHDIEN();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            reset();
            pnThongTin.Visible = false;
            //grbBoChuyenDoiDien.Visible = true;

            tsThem.Enabled = true;
            tsLoaiBoKD.Enabled = true;
        }

        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != ',' && e.KeyChar != '.' && e.KeyChar != (char)46 && e.KeyChar != (char)8)
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                {
                    e.Handled = true;

                }
            }
        }

        BoKichDienDTO bkdDTO1 = new BoKichDienDTO();
        private void gvCDD_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            if (rowIndex >= 0)
            {
                pnThongTin.Visible = true;
                TrangThai(true);

                DataGridViewRow row = gvCDD.Rows[rowIndex];
                bkdDTO1.STT = int.Parse(row.Cells["STT"].Value.ToString());
                txtMaTB.Text = row.Cells["colMaTB"].Value.ToString();
                txtTenTB.Text = row.Cells["colTenTB"].Value.ToString();
                cbbLoaiTB.Text = row.Cells["colTenLoai"].Value.ToString();
                dtpNgayNhap.Text = row.Cells["colNgayNhap"].Value.ToString();
                txtCongSuat.Text = row.Cells["colCSKhaDung"].Value.ToString();
                txtGia.Text = row.Cells["colDonGia"].Value.ToString();

                //dtpNgayNhap.Text = bkdDTO1.NGAYNHAP.ToString();
                //txtMaTB.Text = bkdDTO1.MATB.ToString();
                //cbbLoaiTB.SelectedText = bkdDTO1.TENTB;
                //txtCongSuat.Text = bkdDTO1.CSKHADUNG.ToString();
                //txtGia.Text = bkdDTO1.DONGIA.ToString();
            }
        }

        private void gvCDD_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Bạn có chắc muốn xóa!", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    BoKichDienBUS bkdBUS = new BoKichDienBUS();
                    int stt = Convert.ToInt32(gvCDD.CurrentRow.Cells["STT"].Value);

                    bkdBUS.XoaBoKichDien(stt);
                }
                LoadBoKD();
            }
        }

        private void LoadLoaiBoKD_GV()
        {
            LoaiBoKichDienBUS loaiKD_BUS = new LoaiBoKichDienBUS();
            gvLoaiBoKD.DataSource = loaiKD_BUS.LoadLoaiBoKD();

            SetupDataGridView(gvLoaiBoKD);
        }

        private void txtMaTB_Leave(object sender, EventArgs e)
        {

        }

        private void btnSuaLoai_Click(object sender, EventArgs e)
        {
            LoaiBoKichDienBUS loaiKD_BUS = new LoaiBoKichDienBUS();
            LoaiBoKichDienDTO lkdDTO = new LoaiBoKichDienDTO();

            if (txtLoaiBoKD.Text != "")
            {
                lkdDTO.TENLOAIKD = txtLoaiBoKD.Text.Trim();
                int kt = loaiKD_BUS.KTLoaiKD(lkdDTO);
                if (kt == 0)
                {
                    lkdDTO.MALOAIKD = lkdDTO1.MALOAIKD;
                    loaiKD_BUS.SuaLoaiKD(lkdDTO);

                    txtLoaiBoKD.Text = "";
                    btnThemLoai.Visible = true;
                    btnSuaLoai.Visible = false;
                }
                else
                {
                    MessageBox.Show("Loại " + txtLoaiBoKD.Text + " đã có", "Thông báo");
                }
            }
            else
            {
                MessageBox.Show("Chưa nhập loại!", "Thông báo");
            }

            btnThemLoai.Visible = true;
            btnSuaLoai.Visible = false;
            LoadLoaiBoKD_GV();
        }

        private void btnThemLoai_Click(object sender, EventArgs e)
        {
            LoaiBoKichDienBUS loaiKD_BUS = new LoaiBoKichDienBUS();
            LoaiBoKichDienDTO lkdDTO = new LoaiBoKichDienDTO();

            if (txtLoaiBoKD.Text != "")
            {
                lkdDTO.TENLOAIKD = txtLoaiBoKD.Text.Trim();
                int kt = loaiKD_BUS.KTLoaiKD(lkdDTO);
                if (kt == 0)
                {
                    loaiKD_BUS.ThemLoaiKD(lkdDTO);
                    txtLoaiBoKD.Text = "";
                }
                else
                {
                    MessageBox.Show("Loại " + txtLoaiBoKD.Text + " đã có", "Thông báo");
                }
            }
            else
            {
                MessageBox.Show("Chưa nhập loại!", "Thông báo");
            }

            LoadLoaiBoKD_GV();
        }

        private void btnDongLoai_Click(object sender, EventArgs e)
        {
            tsThem.Enabled = true;
            tsLoaiBoKD.Enabled = true;
            tsDongGrbTT.Enabled = true;

            //TrangThai_LoaiKD(true);
            pnThongTin.Visible = false;
            grbBoChuyenDoiDien.Visible = true;
            pnLoai.Visible = false;

            txtLoaiBoKD.Text = "";
            //reset();
        }

        LoaiBoKichDienDTO lkdDTO1 = new LoaiBoKichDienDTO();
        private void gvLoaiBoKD_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnThemLoai.Visible = false;
            btnSuaLoai.Visible = true;

            int rowIndex = e.RowIndex;
            if (rowIndex >= 0)
            {
                TrangThai(true);

                DataGridViewRow row = gvLoaiBoKD.Rows[rowIndex];
                lkdDTO1.MALOAIKD = int.Parse(row.Cells["colMaLoaiKD"].Value.ToString());
                txtLoaiBoKD.Text = row.Cells["colTenLoaiKD"].Value.ToString();
            }
        }

        private void gvLoaiBoKD_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                LoaiBoKichDienBUS lkdBUS = new LoaiBoKichDienBUS();
                LoaiBoKichDienDTO lkdDTO = new LoaiBoKichDienDTO();
                lkdDTO.MALOAIKD = Convert.ToInt32(gvLoaiBoKD.CurrentRow.Cells["colMaLoaiKD"].Value);
                lkdDTO.TENLOAIKD = gvLoaiBoKD.CurrentRow.Cells["colTenLoaiKD"].Value.ToString();

                int kt = lkdBUS.KTMaLoaiKD(lkdDTO);
                if (kt == 1)//loại KD này đang có sp
                {
                    MessageBox.Show("Loại kích điện " + lkdDTO.TENLOAIKD + " đang có sản phẩm, nên không thể xóa!", "Thông báo");
                }
                else
                if (MessageBox.Show("Bạn có chắc muốn xóa!", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    lkdBUS.XoaLoaiKD(lkdDTO);
                    if (lkdDTO.TENLOAIKD == txtLoaiBoKD.Text)
                        txtLoaiBoKD.Text = "";
                }
                LoadLoaiBoKD_GV();
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            reset();

            //btnThem.Visible = true;
            //btnSua.Visible = false;
        }
    }
}

